﻿using LdtDataEngin.ModuleDetail.BaseModule.Optimization;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.SameNodeConfigModel;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;

namespace LdtDataEngin.ModuleDetail.OptimizationModule.StageOptimization
{
    public class StageOptimizationClass : OptimizationBase
    {
        public bool MergeOp = false;
        /// <summary>
        /// 案由要素优选操作
        /// </summary>
        /// <param name="prentID">开始优选节点</param>
        /// <param name="dataModelsOPList">上次分析结果</param>
        public void StageOptimiztionRun(List<DataModel> dataModelAsisList, string prentID, List<DataModel> dataModelsOPList, OptimizeConfig optimizeStage, string dataSourceOne, string dataSourceTowe)
        {
            OptimizationData optimizationData = new OptimizationData() { dataContext = dataContext };
            optimizationData.DataSourceResult = dataSourceOne;
            optimizationData.DataSourceParent = dataSourceTowe;
            optimizationData.IsStage = 1;
            List<DataModel> dataModelsAissNodeList = new List<DataModel>();
            //获取所有分析结果下级节点信息
            if (dataContext.DataLayerAns.CaseCausesList.Count > 0)
            {
                //dataModelsAissNodeList = dataModelAsisList.Where(a => a.ParentId == prentID && a.XmlPath.IndexOf(dataContext.DataLayerAns.CaseCausesList[0].CC_Name) < 0).ToList();
                dataModelsAissNodeList = dataModelAsisList.Where(a => a.ParentId == prentID && a.XmlPath.IndexOf("案由要素") < 0).ToList();
                foreach (DataModel dataModelAsisNode in dataModelsAissNodeList)
                {
                    List<DataModel> dataModelListAsis = new List<DataModel>();//分析下级循环合集
                    List<DataModel> dataModelListOP = new List<DataModel>();//优选下级循环合集
                    dataModelListAsis = dataModelAsisList;
                    dataModelListOP = dataModelsOPList;
                    Xmllable xmllableCofig = optimizeStage.XmllableList.FirstOrDefault(a => a.Xmlpath == dataModelAsisNode.XmlPath);
                    if (xmllableCofig != null)
                    {
                        DataModel dataModelOpSging = new DataModel();
                        try
                        {
                            if (xmllableCofig.Type == "a")
                            {
                                //标识优选需要找出对应标识的节点信息
                                foreach (Signnode element in xmllableCofig.SignnodeList.NodeList)
                                {
                                    string elementvalue = dataModelAsisNode.AttributesList.FirstOrDefault(a => a.Name == element.Code).sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                    dataModelOpSging = dataModelListOP.FirstOrDefault(a => a.XmlPath == xmllableCofig.Xmlpath && a.AttributesList.FirstOrDefault(b => b.Name == element.Code &&
                                    b.sourceList.FirstOrDefault(s => s.IsCheck == "1" && s.SaveValue == elementvalue) != null) != null);
                                    if (dataModelOpSging != null)
                                    {
                                        dataModelListOP = SubordinateDataModelList(dataModelsOPList, dataModelOpSging);
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                dataModelOpSging = dataModelListOP.FirstOrDefault(a => a.XmlPath == xmllableCofig.Xmlpath);
                            }
                            optimizationData.OptimizationDataRun(dataModelAsisNode, dataModelOpSging, xmllableCofig, optimizeStage, dataModelListOP, dataModelAsisList);
                        }
                        catch (System.Exception)
                        {
                            dataContext.InteractiveShow.LogShow("阶段性优选错误");
                        }
                    }
                    else
                    {
                        DataModel dataModelOpSging = dataModelListOP.FirstOrDefault(a => a.XmlPath == dataModelAsisNode.XmlPath);
                        if (dataModelOpSging == null)
                        {
                            //获取上级节点信息
                            string parentxml = "";
                            List<string> pathlist = dataModelAsisNode.XmlPath.Split('/').ToList();
                            pathlist.RemoveAt(pathlist.Count - 1);
                            parentxml = string.Join("/", pathlist);
                            CaseNodeUpdate(dataModelListOP, parentxml);
                            DataModel nodeParent = dataModelListOP.FirstOrDefault(a => a.XmlPath == parentxml);
                            string message = JsonConvert.SerializeObject(dataModelAsisNode);
                            DataModel dataModelAddNode = JsonConvert.DeserializeObject<DataModel>(message);
                            dataModelAddNode.ParentId = nodeParent.ID;
                            dataModelListOP.Add(dataModelAddNode);
                        }
                        else
                        {
                            List<DataModel> opList = dataModelListOP.Where(a => a.XmlPath == dataModelAsisNode.XmlPath).ToList();
                            if (opList.Count > 0)
                            {
                                if (!ISsameNode(opList, dataModelAsisNode))
                                {
                                    //不存在该节点
                                    string parentxml = "";
                                    List<string> pathlist = dataModelAsisNode.XmlPath.Split('/').ToList();
                                    pathlist.RemoveAt(pathlist.Count - 1);
                                    parentxml = string.Join("/", pathlist);
                                    if (!string.IsNullOrEmpty(parentxml))
                                    {
                                        CaseNodeUpdate(dataModelListOP, parentxml);
                                        DataModel nodeParent = dataModelListOP.FirstOrDefault(a => a.XmlPath == parentxml);
                                        string message = JsonConvert.SerializeObject(dataModelAsisNode);
                                        DataModel dataModelAddNode = JsonConvert.DeserializeObject<DataModel>(message);
                                        dataModelAddNode.ParentId = nodeParent.ID;
                                        dataModelListOP.Add(dataModelAddNode);
                                    }
                                }
                            }
                        }
                    }
                    StageOptimiztionRun(dataModelAsisList, dataModelAsisNode.ID, dataModelsOPList, optimizeStage, dataSourceOne, dataSourceTowe);
                }
            }
            else
            {
                dataModelsAissNodeList = dataModelAsisList.Where(a => a.ParentId == prentID).ToList();
                if (dataModelsAissNodeList.Count > 0)
                {
                    foreach (DataModel dataModelAsisNode in dataModelsAissNodeList)
                    {
                        List<DataModel> dataModelListAsis = new List<DataModel>();//分析下级循环合集
                        List<DataModel> dataModelListOP = new List<DataModel>();//优选下级循环合集
                        dataModelListAsis = dataModelAsisList;
                        dataModelListOP = dataModelsOPList;
                        Xmllable xmllableCofig = optimizeStage.XmllableList.FirstOrDefault(a => a.Xmlpath == dataModelAsisNode.XmlPath);
                        if (xmllableCofig != null)
                        {
                            DataModel dataModelOpSging = new DataModel();

                            if (xmllableCofig.Type == "a")
                            {
                                foreach (Element element in xmllableCofig.ElementList)
                                {
                                    if (dataModelAsisNode.AttributesList.FirstOrDefault(a => a.Name == element.Name) != null)
                                    {
                                        string elementvalue = dataModelAsisNode.AttributesList.FirstOrDefault(a => a.Name == element.Name).sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                        dataModelOpSging = dataModelListOP.FirstOrDefault(a => a.XmlPath == xmllableCofig.Xmlpath && a.AttributesList.FirstOrDefault(b => b.Name == element.Name &&
                                        b.sourceList.FirstOrDefault(s => s.IsCheck == "1" && s.SaveValue == elementvalue) != null) != null);
                                        if (dataModelOpSging != null)
                                        {
                                            dataModelListOP = SubordinateDataModelList(dataModelsOPList, dataModelOpSging);
                                        }
                                        break;
                                    }
                                    else
                                    {
                                        dataModelOpSging = null;
                                    }
                                }
                            }
                            else
                            {
                                dataModelOpSging = dataModelListOP.FirstOrDefault(a => a.XmlPath == xmllableCofig.Xmlpath);
                            }
                            optimizationData.OptimizationDataRun(dataModelAsisNode, dataModelOpSging, xmllableCofig, optimizeStage, dataModelListOP, dataModelAsisList);
                        }
                        else
                        {
                            DataModel dataModelOpSging = dataModelListOP.FirstOrDefault(a => a.XmlPath == dataModelAsisNode.XmlPath);
                            if (dataModelOpSging == null)
                            {

                                //获取上级节点信息
                                string parentxml = "";
                                List<string> pathlist = dataModelAsisNode.XmlPath.Split('/').ToList();
                                pathlist.RemoveAt(pathlist.Count - 1);
                                parentxml = string.Join("/", pathlist);
                                if (!string.IsNullOrEmpty(parentxml))
                                {

                                    CaseNodeUpdate(dataModelListOP, parentxml);
                                    DataModel nodeParent = dataModelListOP.FirstOrDefault(a => a.XmlPath == parentxml);
                                    string message = JsonConvert.SerializeObject(dataModelAsisNode);
                                    DataModel dataModelAddNode = JsonConvert.DeserializeObject<DataModel>(message);
                                    dataModelAddNode.ParentId = nodeParent.ID;
                                    dataModelListOP.Add(dataModelAddNode);
                                }
                            }
                            else
                            {
                                List<DataModel> opList = dataModelListOP.Where(a => a.XmlPath == dataModelAsisNode.XmlPath).ToList();
                                if (opList.Count > 0)
                                {
                                    if (!ISsameNode(opList, dataModelAsisNode))
                                    {
                                        //不存在该节点
                                        string parentxml = "";
                                        List<string> pathlist = dataModelAsisNode.XmlPath.Split('/').ToList();
                                        pathlist.RemoveAt(pathlist.Count - 1);
                                        parentxml = string.Join("/", pathlist);
                                        if (!string.IsNullOrEmpty(parentxml))
                                        {
                                            CaseNodeUpdate(dataModelListOP, parentxml);
                                            DataModel nodeParent = dataModelListOP.FirstOrDefault(a => a.XmlPath == parentxml);
                                            string message = JsonConvert.SerializeObject(dataModelAsisNode);
                                            DataModel dataModelAddNode = JsonConvert.DeserializeObject<DataModel>(message);
                                            dataModelAddNode.ParentId = nodeParent.ID;
                                            dataModelListOP.Add(dataModelAddNode);
                                        }

                                    }
                                }
                            }
                        }
                        StageOptimiztionRun(dataModelAsisList, dataModelAsisNode.ID, dataModelsOPList, optimizeStage, dataSourceOne, dataSourceTowe);
                    }
                }

            }
        }


        /// <summary>
        /// 案件信息优选
        /// </summary>
        /// <param name="dataModelAsisList"></param>
        /// <param name="prentID"></param>
        /// <param name="dataModelsOPList"></param>
        public void CaseElmentOptimization(List<DataModel> dataModelAsisList, string prentID, List<DataModel> dataModelsOPList, OptimizeConfig optimizeStage, SameNodeConfig sameNodeConfig=null)
        {
            OptimizationData optimizationData = new OptimizationData() { dataContext = dataContext };
            optimizationData.IsStage = 1;
            List<DataModel> dataModelsAissNodeList = new List<DataModel>();
            EvidenceUnionDataModel evidenceUnionData = new EvidenceUnionDataModel();
            XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
            if (dataContext.DataLayerAns.ExtractCaseCausesList != null && dataContext.DataLayerAns.ExtractCaseCausesList.Count > 0)
            {
                if (!MergeOp)
                {
                    dataModelsAissNodeList = dataModelAsisList.Where(a => a.ParentId == prentID  && !a.XmlPath.Contains("分人员要素") && !a.XmlPath.Contains("合并后要素")).ToList();
                }
                else
                {
                    dataModelsAissNodeList = dataModelAsisList.Where(a => a.ParentId == prentID).ToList();
                }
               
            }
            else
            {
                dataModelsAissNodeList = dataModelAsisList.Where(a => a.ParentId == prentID).ToList();
            }
            if (dataModelsAissNodeList.Count > 0)
            {
                EvidenceUnionDataModel unionDataModel = new EvidenceUnionDataModel();
                foreach (DataModel dataModelAsisNode in dataModelsAissNodeList)
                {
                    foreach (var attmode in dataModelAsisNode.AttributesList)
                    {
                        if (attmode.sourceList!=null && attmode.sourceList.Count>0 && attmode.sourceList.FirstOrDefault(a=> a.IsCheck=="1")==null)
                        {
                            attmode.sourceList[0].IsCheck = "1";
                        }
                    }
                    Xmllable xmllableCofigs = optimizeStage.XmllableList.FirstOrDefault(a => a.Xmlpath == dataModelAsisNode.XmlPath);
                    if (xmllableCofigs != null && xmllableCofigs.Type == "b")
                    {
                        //单一数据源优选
                        #region 单一数据源
                        string parentid = "";
                        DataModel dataOpModel = new DataModel();
                        if (dataModelsOPList.FirstOrDefault(a => a.XmlPath == xmllableCofigs.Xmlpath && a.State != "2") == null)
                        {
                            string xmlpath = "";
                            for (int i = 0; i < xmllableCofigs.Xmlpath.Split('/').Length - 1; i++)
                            {
                                if (xmlpath == "")
                                {
                                    xmlpath = xmllableCofigs.Xmlpath.Split('/')[i];
                                }
                                else
                                {
                                    xmlpath = xmlpath + "/" + xmllableCofigs.Xmlpath.Split('/')[i];

                                }
                            }
                            if (dataModelsOPList.FirstOrDefault(a => a.XmlPath == xmlpath && a.State != "2") != null)
                            {
                                parentid = dataModelsOPList.FirstOrDefault(a => a.XmlPath == xmlpath).ID;
                                dataModelAsisNode.ParentId = parentid;
                                List<DataModel> dataModelListRestAiss = SubordinateDataModelList(dataModelAsisList, dataModelAsisNode);
                                dataModelListRestAiss = JsonConvert.DeserializeObject<List<DataModel>>(JsonConvert.SerializeObject(dataModelListRestAiss));
                                RestructureElment(dataModelListRestAiss, dataModelAsisNode.ParentId, dataModelAsisNode.ParentId);
                                foreach (DataModel aissmodel in dataModelListRestAiss)
                                {
                                    aissmodel.State = "1";
                                    dataModelsOPList.Add(aissmodel);
                                }
                            }
                        }
                        else
                        {
                            dataOpModel = dataModelsOPList.FirstOrDefault(a => a.XmlPath == xmllableCofigs.Xmlpath && a.State != "2");
                            string modesours = dataOpModel.sourceData.sourceList.FirstOrDefault(a => a.IsCheck == "1").SourceCode;
                            string soursnameopid = optimizeStage.Sourcedatadefine[0].SourcedataList.FirstOrDefault(a => a.Code.ToLower() == modesours.ToLower()).id.ToString();
                            string soursnameasisid = optimizeStage.Sourcedatadefine[0].SourcedataList.FirstOrDefault(a => a.Code.ToLower() == dataModelAsisNode.sourceData.sourceList.FirstOrDefault(a => a.IsCheck == "1").SourceCode.ToLower()).id.ToString();
                            int opsum = ListByCount(xmllableCofigs.Priority.Split('>').ToList(), soursnameopid);
                            int assum = ListByCount(xmllableCofigs.Priority.Split('>').ToList(), soursnameasisid);
                            if (assum <= opsum)
                            {
                                if (dataModelsOPList.FirstOrDefault(a => a.XmlPath == xmllableCofigs.Xmlpath && a.State != "2") != null)
                                {
                                    List<DataModel> dataModelListOp = dataModelsOPList.Where(a => a.XmlPath == xmllableCofigs.Xmlpath && a.State != "2").ToList();
                                    foreach (DataModel dataop in dataModelListOp)
                                    {
                                        List<DataModel> dataModelListRest = SubordinateDataModelList(dataModelsOPList, dataop);
                                        parentid = dataop.ParentId;
                                        foreach (DataModel dataModelOP in dataModelListRest)
                                        {
                                            dataModelOP.State = "2";
                                        }
                                    }
                                }

                                List<DataModel> dataModels = dataModelsAissNodeList.Where(a => a.XmlPath == dataModelAsisNode.XmlPath)?.ToList();
                                if (dataModels!=null)
                                {
                                    foreach (DataModel AsisNode in dataModels)
                                    {
                                        List<DataModel> dataModelListRestAiss = SubordinateDataModelList(dataModelAsisList, AsisNode);
                                        dataModelListRestAiss.FirstOrDefault(a => a.ID == AsisNode.ID).ParentId = parentid;
                                        dataModelListRestAiss = JsonConvert.DeserializeObject<List<DataModel>>(JsonConvert.SerializeObject(dataModelListRestAiss));
                                        RestructureElment(dataModelListRestAiss, AsisNode.ParentId, AsisNode.ParentId);

                                        foreach (DataModel aissmodel in dataModelListRestAiss)
                                        {
                                            aissmodel.State = "1";
                                            dataModelsOPList.Add(aissmodel);
                                        }
                                    }
                                }
                               
                               

                            }
                            //if (assum == opsum)
                            //{
                            //    if (dataModelsOPList.FirstOrDefault(a => a.XmlPath == xmllableCofigs.Xmlpath && a.State != "2") != null)
                            //    {

                            //        DataModel modelOP = dataModelsOPList.FirstOrDefault(a => a.XmlPath == xmllableCofigs.Xmlpath && a.State != "2");
                            //        if (dataModelsAissNodeList.Count > 0)
                            //        {
                            //            modelOP.sourceData = dataModelAsisNode.sourceData;
                            //            modelOP.AttributesList = dataModelAsisNode.AttributesList;
                            //        }

                            //    }
                            //}
                        }

                        #endregion
                    }
                    else
                    {
                        #region 多数据源优选
                        List<DataModel> dataModelListAsis = new List<DataModel>();//分析下级循环合集
                        List<DataModel> dataModelListOP = new List<DataModel>();//优选下级循环合集
                        dataModelListAsis = dataModelAsisList;
                        dataModelListOP = dataModelsOPList;
                        Xmllable xmllableCofig = optimizeStage.XmllableList.FirstOrDefault(a => a.Xmlpath == dataModelAsisNode.XmlPath);
                        if (xmllableCofig != null)
                        {
                            bool determine = true;
                            if (xmllableCofig.ConditionList.Count > 0)
                            {
                                try
                                {
                                    determine = ConditionDetermine(xmllableCofig.ConditionList);
                                }
                                catch (System.Exception ef)
                                {
                                    dataContext.InteractiveShow.LogShow("当前数据项条件解析错误，配置信息：" + JsonConvert.SerializeObject(ef.ToString()));
                                    dataContext.InteractiveShow.LogShow("当前数据项条件解析错误，错误信息：" + ef.ToString());
                                }
                            }
                            if (determine)
                            {
                                DataModel dataModelOpSging = new DataModel();
                                if (xmllableCofig.Type == "a")
                                {
                                    Dictionary<string, string> sginDic = new Dictionary<string, string>();
                                    foreach (var element in xmllableCofig.SignnodeList.NodeList)
                                    {
                                        if (dataModelAsisNode.AttributesList.FirstOrDefault(a => a.Name == element.Code) != null)
                                        {
                                            string elementvalue = dataModelAsisNode.AttributesList.FirstOrDefault(a => a.Name == element.Code).sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                                            sginDic.Add(element.Code, elementvalue);
                                        }
                                        else
                                        {
                                            sginDic.Add(element.Code, null);
                                        }
                                    }
                                    List<DataModel> dataSingList = new List<DataModel>();
                                    if (dataModelListOP.FirstOrDefault(a => a.XmlPath == xmllableCofig.Xmlpath ) != null)
                                    {
                                        dataSingList = dataModelListOP.Where(a => a.XmlPath == xmllableCofig.Xmlpath ).ToList();
                                        foreach (var dic in sginDic)
                                        {
                                            if (dic.Value == null)
                                            {
                                                if (dataSingList.FirstOrDefault(a => a.AttributesList.FirstOrDefault(b => b.Name == dic.Key) == null) != null)
                                                {
                                                    dataSingList = dataSingList.Where(a => (a.XmlPath == xmllableCofig.Xmlpath) && a.AttributesList.FirstOrDefault(b => b.Name == dic.Key) == null).ToList();
                                                }
                                                else
                                                {
                                                    dataSingList = new List<DataModel>();
                                                }

                                            }
                                            else
                                            {
                                                try
                                                {
                                                    if (dataModelListOP.FirstOrDefault(a => a.AttributesList!=null && a.AttributesList.FirstOrDefault(b => b.Name == dic.Key && b.sourceList.FirstOrDefault(s => s.IsCheck == "1" && s.SaveValue == dic.Value) != null) != null) != null)
                                                    {
                                                        dataSingList = dataSingList.Where(a => a.AttributesList.FirstOrDefault(b => b.Name == dic.Key &&
                                                      b.sourceList.FirstOrDefault(s => s.IsCheck == "1" && s.SaveValue == dic.Value) != null) != null).ToList();
                                                    }
                                                    else
                                                    {
                                                        dataSingList = new List<DataModel>();
                                                    }
                                                }
                                                catch (System.Exception eff)
                                                {

                                                    
                                                }
                                            }

                                        }
                                        if (dataSingList != null && dataSingList.Count > 0)
                                        {
                                            dataModelOpSging = dataSingList.First();
                                            if (dataModelOpSging != null)
                                            {
                                                if (dataModelAsisNode.XmlPath == "详情/证据相关信息/证据/详情/证据内容/详情")
                                                {
                                                    var evidAsisList = SubordinateDataModelList(dataModelAsisList, dataModelAsisNode);
                                                    //获取证据内容节点的ID
                                                    var opEvidConentNodeId = dataModelsOPList.Where(x => x.XmlPath.EndsWith("证据内容")).FirstOrDefault()?.ID;
                                                    xmlUtilCommon.DataModelIDByReset(evidAsisList, dataModelAsisNode.ParentId, opEvidConentNodeId);
                                                    if (evidAsisList.Any(x => x.XmlPath.Contains("案由要素")))
                                                    {
                                                        //通过证据内容详情的同节点配置判断是否存在相同证据
                                                        if (sameNodeConfig != null)
                                                        {
                                                            //获取传入阶段的相同证据要素数据
                                                            var opEvidList = SubordinateDataModelList(dataModelsOPList, dataModelOpSging);
                                                            dataModelListOP = dataModelListOP.Except(opEvidList).ToList();
                                                            //合并证据数据
                                                            evidenceUnionData.MergeNode(evidAsisList, dataModelAsisNode.ParentId,opEvidList, dataModelOpSging.ParentId, sameNodeConfig.xmllable.ToList(), "c");
                                                            dataModelListOP.AddRange(opEvidList);
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    dataModelListOP = SubordinateDataModelList(dataModelsOPList, dataModelOpSging);
                                                    List<DataModel> parentNodeModel = ParendNodeDataModelList(dataModelsOPList, dataModelOpSging);
                                                    dataModelListOP.AddRange(parentNodeModel);
                                                }
                                            }
                                            
                                        }
                                        else
                                        {
                                            if (dataModelAsisNode.XmlPath == "详情/证据相关信息/证据/详情/证据内容/详情")
                                            {
                                                var evidAsisList = SubordinateDataModelList(dataModelAsisList, dataModelAsisNode);
                                                //获取证据内容节点的ID
                                                var opEvidConentNodeId = dataModelsOPList.Where(x => x.XmlPath.EndsWith("证据内容")).FirstOrDefault()?.ID;
                                                xmlUtilCommon.DataModelIDByReset(evidAsisList, dataModelAsisNode.ParentId, opEvidConentNodeId);
                                                if (evidAsisList.Any(x => x.XmlPath.Contains("案由要素")))
                                                {
                                                    dataModelListOP.AddRange(evidAsisList);
                                                }
                                            }
                                        }
                                    }

                                }
                                else
                                {
                                    dataModelOpSging = dataModelListOP.FirstOrDefault(a => a.XmlPath == xmllableCofig.Xmlpath && a.State != "2");
                                }
                                
                                optimizationData.OptimizationDataRun(dataModelAsisNode, dataModelOpSging, xmllableCofig, optimizeStage, dataModelListOP, dataModelAsisList);
                            }
                            CaseElmentOptimization(dataModelAsisList, dataModelAsisNode.ID, dataModelListOP, optimizeStage,sameNodeConfig);
                            foreach (DataModel opModel in dataModelListOP)
                            {
                                if (dataModelsOPList.FirstOrDefault(a => a.ID == opModel.ID && a.State == opModel.State) != null)
                                {

                                    DataModel model = dataModelsOPList.FirstOrDefault(a => a.ID == opModel.ID);
                                    model = opModel;

                                }
                                else
                                {
                                    dataModelsOPList.Add(opModel);
                                }
                            }
                        }
                        else
                        {

                            CaseElmentOptimization(dataModelAsisList, dataModelAsisNode.ID, dataModelsOPList, optimizeStage,sameNodeConfig);
                        }


                        #endregion
                    }
                }
            }
        }
    }
}
